<template>
  <div class="app-container">

    <el-button v-if="tableVisible" class="filter-item" style="margin-bottom: 10px;" type="primary" icon="el-icon-edit" @click="changeVisible">
      创建按钮
    </el-button>
    <template v-if="formVisible">
      <el-form ref="dataForm" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px;">
        <el-form-item label="按钮名称" prop="name">
          <el-input v-model="fromTemp.buttonName" />
        </el-form-item>
        <el-form-item label="按钮标识" prop="icon">
          <el-input v-model="fromTemp.attr" />
        </el-form-item>
        <el-form-item label="访问地址" prop="url">
          <el-input v-model="fromTemp.url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changeVisible">
          取消
        </el-button>
        <el-button type="primary" @click="saveMenuButton">
          确认
        </el-button>
      </div>
    </template>

    <template v-if="tableVisible">
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="buttonName"
          label="按钮名称"
          width="180"
        />
        <el-table-column
          prop="attr"
          label="按钮标识"
          width="180"
        />
        <el-table-column
          prop="url"
          label="访问地址"
        />
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="{row}">
            <AuthButton auth-name="DeleteItemButton">
              <el-button type="danger" @click="deleteButton(row)">删除 </el-button>
            </AuthButton>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
import { GetMenuButtonList, SaveMenuButton, DeleteMenuButton } from '@/api/SystemManagerApi'
import AuthButton from '@/views/authButton/Button';

export default {
  // eslint-disable-next-line vue/require-prop-types
  components: { AuthButton },
  props: ['menuid'],
  data() {
    return {
      formVisible: false,
      tableVisible: true,
      fromTemp: {
        id: null,
        buttonName: '',
        attr: '',
        menuId: '',
        url: ''
      },
      list: []
    }
  },
  watch: {
    menuid: {
      immediate: true, // 很重要！！！
      handler(val) {
        this.menuid = val
        this.getList()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeVisible() {
      if (this.formVisible) {
        this.formVisible = false
        this.tableVisible = true
      } else {
        this.formVisible = true
        this.tableVisible = false
      }

      this.getList()
    },
    getList() {
      this.listLoading = true
      GetMenuButtonList(this.menuid).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    saveMenuButton() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.fromTemp.menuId = this.menuid

          SaveMenuButton(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.changeVisible()
          })
        }
      })
    },
    deleteButton(row) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteMenuButton({ id: row.id }).then((res) => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
